﻿//tyh添加
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Estimate.Common.Entity;
using Estimate.Common.DBFieldTypes;
using System.Text.RegularExpressions;

namespace Estimate.Common.Estimate
{

    /// <summary>
    /// 根据预报的结果，获得预报的误差方向
    /// </summary>
    public class DeviationResult
    {
        public DeviationKind kind = DeviationKind.Ignore;

        public float X = 0f;

        public float Y = 0f;

        public int LackCount = 0;

        public int EmptyCount = 0;

        public DeviationResult()
        {
 
        }
        
         //给定偏差类型
        public DeviationResult(DeviationKind k)
        {
            kind = k;
        }

        /// <summary>
        /// 设置偏差结果
        /// </summary>
        /// <param name="x">x轴方向偏差</param>
        /// <param name="y">y轴方向偏差</param>
        /// <param name="lackCount">缺少数量</param>
        /// <param name="emptyCount">空格数量</param>
        public DeviationResult(float x, float y, int lackCount, int emptyCount)
        {
            X = x;
            Y = y;
            LackCount = lackCount;
            EmptyCount = emptyCount;
            kind = DeviationKind.Deviation;
        }

        public DeviationDirection Direction
        {
            get
            {
                double angle = Math.Atan2(this.Y, this.X) * (180 / Math.PI);
                if (angle < 0)
                    angle += 360;

                if (angle >= 0 && angle < 22.5)
                    return DeviationDirection.East;
                else if (angle >= 22.5 && angle < 67.5)
                    return DeviationDirection.EastNorth;
                else if (angle >= 67.5 && angle < 112.5)
                    return DeviationDirection.North;
                else if (angle >= 112.5 && angle < 157.5)
                    return DeviationDirection.WestNorth;
                else if (angle >= 157.5 && angle < 202.5)
                    return DeviationDirection.West;
                else if (angle >= 202.5 && angle < 247.5)
                    return DeviationDirection.SouthWest;
                else if (angle >= 247.5 && angle < 292.5)
                    return DeviationDirection.Sourh;
                else if (angle >= 292.5 && angle < 337.5)
                    return DeviationDirection.EastSouth;
                else
                    return DeviationDirection.East;
            }
        }

        //预报偏大偏小
        public DeviationLevel Level
        {
            get
            {
                if (this.EmptyCount > this.LackCount)
                    return DeviationLevel.Big;
                else if (this.LackCount > this.EmptyCount)
                    return DeviationLevel.Small;
                else
                    return DeviationLevel.Normal;
            }
        }

        public static DeviationResult operator +(DeviationResult d1, DeviationResult d2)
        {
            return new DeviationResult(d1.X + d2.X, d1.Y + d2.Y, d1.LackCount + d2.LackCount, d1.EmptyCount + d2.EmptyCount);
        }

        public override string ToString()
        {    
            return string.Format("{0},{1},{2},{3},{4}", new object[]{
                        this.kind,
                        this.X,
                        this.Y,
                        this.LackCount,
                        this.EmptyCount
                    });
        }

        public void FromString(string value)
        {
            string[] valueArray = Regex.Split(value, @"\,");
            kind = (DeviationKind)Enum.Parse(typeof(DeviationKind), valueArray[0]);
            X = Convert.ToSingle(valueArray[1]);
            Y = Convert.ToSingle(valueArray[2]);
            LackCount = Convert.ToInt32(valueArray[3]);
            EmptyCount = Convert.ToInt32(valueArray[4]);
        }


        //public DeviationResult(DeviationKind k, float x, float y, int lackCount, int emptyCount)
        //{
        //    kind = k;
        //    X = x;
        //    Y = y;
        //    LackCount = lackCount;
        //    EmptyCount = emptyCount;
        //}

        //public  DeviationDirection Direction()
        //{

        //    double angle = Math.Atan2(Y, X) * (180 / Math.PI);
        //    if (angle < 0)
        //        angle += 360;

        //    if (angle >= 0 && angle < 22.5)
        //        return DeviationDirection.East;
        //    else if (angle >= 22.5 && angle < 67.5)
        //        return DeviationDirection.EastNorth;
        //    else if (angle >= 67.5 && angle < 112.5)
        //        return DeviationDirection.North;
        //    else if (angle >= 112.5 && angle < 157.5)
        //        return DeviationDirection.WestNorth;
        //    else if (angle >= 157.5 && angle < 202.5)
        //        return DeviationDirection.West;
        //    else if (angle >= 202.5 && angle < 247.5)
        //        return DeviationDirection.SouthWest;
        //    else if (angle >= 247.5 && angle < 292.5)
        //        return DeviationDirection.Sourh;
        //    else if (angle >= 292.5 && angle < 337.5)
        //        return DeviationDirection.EastSouth;
        //    else
        //        return DeviationDirection.East;

        //}

        //public DeviationDirection direction
        //{
        //    get
        //    {
        //        return Direction();
        //    }
        //}
    }
}
